/* ****************************************************************************
 * Author: 671643387
 * Date: 2015/3/18
 * ***************************************************************************/

Ext.define("sggm.controller.GMPanel", {
    extend: "Ext.app.Controller",

    refs: [{
        ref: "gmPanel",
        selector: "gmPanel"
    }, {
        ref: "userBasicDataEditor",
        selector: "userBasicDataEditor"
    }, {
        ref: "userBasicDataSingleEditor",
        selector: "userBasicDataSingleEditor"
    }, {
        ref: "updateMarkWindow",
        selector: "updateMarkWindow"
    }, {
        ref: "sendInventoryWindow",
        selector: "sendInventoryWindow"
    }, {
        ref: "sendEquipWindow",
        selector: "sendEquipWindow"
    }, {
        ref: "updateFighterWindow",
        selector: "updateFighterWindow"
    }, {
        ref: "sendFighterWindow",
        selector: "sendFighterWindow"
    }, {
        ref: "updateVipWindow",
        selector: "updateVipWindow"
    }, {
        ref: "chargeWindow",
        selector: "chargeWindow"
    }, {
        ref: "updateLeague",
        selector: "updateLeague"
    }, {
        ref: "otherWindow",
        selector: "otherWindow"
    }, {
        ref: "changeRoleWindow",
        selector: "changeRoleWindow"
    }, {
        ref: "editCustomWindow",
        selector: "editCustomWindow"
    }, {
        ref: "addCashWindow",
        selector: "addCashWindow"
    }, {
        ref: "setUserWindow",
        selector: "setUserWindow"
    }],

    stores: [
        'UserInfo',
        'UserStatus',
        'UserMark',
        'UserSoldier',
        'UserInventory',
        'Message'
    ],

    init: function() {
        this.control({
            'gmPanel button[action=queryUserInfo]': {
                click: this.onQueryUserInfo
            },

            'gmPanel button[action=logout]': {
                click: this.onLogout
            },
            'gmPanel button[action=editUserBasicData]': {
                click: this.onEditUserBasicData
            },
            'gmPanel button[action=editUserBasicDataSingle]': {
                click: this.onEditUserBasicDataSingle
            },
            'gmPanel button[action=basicAddCash]': {
                click: this.onAddCash
            },
            'gmPanel button[action=editCustom]': {
                click: this.onEditCustom
            },

            'gmPanel button[action=updateUserMark]': {
                click: this.onUpdateUserMark
            },
            'gmPanel button[action=updateUserTask]': {
                click: this.onUpdateUserTask
            },
            'gmPanel button[action=sendInventory]': {
                click: this.onShowSendInventory
            },
            'gmPanel button[action=showSendEquipWindow]': {
                click: this.onShowSendEquipWindow
            },
            'gmPanel button[action=updateFighter]': {
                click: this.onShowUpdateFighter
            },
            'gmPanel button[action=sendFighter]': {
                click: this.onShowSendFighter
            },
            'gmPanel button[action=updateVIP]': {
                click: this.onShowUpdateVIP
            },
            'gmPanel button[action=charge]': {
                click: this.onShowCharge
            },
            'gmPanel button[action=updateLeague]': {
                click: this.onShowUpdateLeague
            },
            'gmPanel button[action=otherCmd]': {
                click: this.onShowOtherCmd
            },
            'gmPanel button[action=changeRole]': {
                click: this.onShowChangeRole
            },

            'gmPanel button[action=showJSON]': {
                click: this.onShowJSON
            },

            'gmPanel button[action=blockUser]': {
                click: this.onBlockUser
            },
            'gmPanel button[action=unblockUser]': {
                click: this.onUnblockUser
            },
            'gmPanel button[action=unblockLimitUser]': {
                click: this.onUnblockLimitUser
            },
            'gmPanel button[action=blockUserTalk]': {
                click: this.onBlockUserTalk
            },
            'gmPanel button[action=unblockUserTalk]': {
                click: this.onUnblockUserTalk
            },
            'gmPanel button[action=clearUserInventory]': {
                click: this.onClearUserInventory
            },
            'gmPanel button[action=diamond]': {
                click: this.onDiamond
            },
            'gmPanel button[action=queryUserMessage]': {
                click: this.onQueryUserMessage
            },
            'gmPanel button[action=autoStrong]': {
                click: this.onAutoStrong
            },
            'gmPanel button[action=setUser]': {
                click: this.onSetUser
            },

            'userBasicDataEditor button[action=saveStatus]' : {
                click: this.onSaveUserStatus
            },
            'userBasicDataEditor button[action=reloadStatus]' : {
                click: this.onReloadUserStatus
            },

            'userBasicDataSingleEditor button[action=updateUserDataSingle]' : {
                click: this.onUpdateUserDataSingle
            },

            'updateMarkWindow button[action=saveUserMark]' : {
                click: this.onSaveUserMark
            },

            'updateTaskWindow button[action=saveUserTask]' : {
                click: this.onSaveUserTask
            },

            'sendInventoryWindow > combo' : {
                afterrender: this.onSendInventoryWindowCombosRender
            },
            'sendInventoryWindow button[action=sendInventory]' : {
                click: this.onSendInventory
            },

            'sendEquipWindow button[action=sendEquip]' : {
                click: this.onSendEquip
            },

            'updateFighterWindow > combo' : {
                afterrender: this.onUpdateFighterWindowComboRender
            },

            'updateFighterWindow button[action=updateFighter]' : {
                click: this.onUpdateFighter
            },

            'sendFighterWindow > combo' : {
                afterrender: this.onSendFighterWindowComboRender
            },
            'sendFighterWindow button[action=sendFighter]' : {
                click: this.onSendFighter
            },

            'updateVipWindow button[action=saveVIP]' : {
                click: this.onSaveVIP
            },

            'chargeWindow button[action=charge]' : {
                click: this.onCharge
            },

            'updateLeague button[action=saveLeague]' : {
                click: this.onSaveLeague
            },

            'otherWindow button[action=saveOther]' : {
                click: this.onSaveOther
            },

            'changeRoleWindow button[action=changeRole]' : {
                click: this.onChangeRole
            },

            'editCustomWindow button[action=saveEditCustom]' : {
                click: this.onSaveEditCustom
            },

            'gmPanel' : {
                afterrender: this.onGMPanelAfterRender
            },

            'addCashWindow button[action=addCash]' : {
                click: this.onAddCashWindowAddCash
            },

            'setUserWindow button[action=saveSetUser]' : {
                click: this.onSaveSetUser
            }
        });

        //this.callParent(arguments);
    },

    onLogout: function() {
        this.getApplication().logout();
    },

    onGMPanelAfterRender: function() {
        console.log("onGMPanelAfterRender");
        this.onQueryUserInfo();
    },

    /**
     * 查询用户数据
     */
    onQueryUserInfo: function() {
        console.log("onQueryUserInfo");

        if (!this.getApplication().checkAccess('get')) {
            Ext.MessageBox.alert("警告", "你没有该操作的访问权限！");
            return;
        }

        var me = this;
        var userQueryForm = Ext.getCmp("gmPanel.userQuery.form");
        var queryParams = userQueryForm.getForm().getValues();
        var reqParams = {
            method: 'get'
        };
        Ext.apply(reqParams, queryParams);
        sggm.utils.Http.request({
            act: 'gm',
            params: reqParams
        }, function(res){

            if (res["code"] == 0) { // 操作成功

                me.getApplication().playerData = res["data"];
                sggm.utils.Http.openid = reqParams["openid"];
                sggm.utils.Http.uid = reqParams["uid"];
                sggm.utils.Http.key = reqParams["key"];

                var sgConfigs = me.getApplication().sgConfigs;

                var data = {
                    totalProperty: 0,
                    root: []
                };

                /* 显示用户信息 */
                data.totalProperty = 1;
                data.root[0] = res["data"]["info"];
                // 最后登录
                if (res["data"].hasOwnProperty("active_time")) {
                    data.root[0]["lastActivie"] =
                        (+res["data"]["active_time"]) + (+res["data"]["info"]["create"]);
                }
                me.getUserInfoStore().proxy.data = data;
                me.getUserInfoStore().load();

                /* 显示用户基础数据 */
                data.totalProperty = 1;
                data.root[0] = res["data"]["status"];
                me.getUserStatusStore().proxy.data = data;
                me.getUserStatusStore().load();

                /* 显示用户标志数据 */
                data.totalProperty = 1;
                data.root[0] = res["data"]["mark"];
                me.getUserMarkStore().proxy.data = data;
                me.getUserMarkStore().load();

                /* 显示玩家武将数据 */
                data.root = [];
                var roleCfg = sgConfigs["role"];
                var roles = res["data"]["role"];
                for (var rId in roles) {
                    if (roles.hasOwnProperty(rId)) {
                        var role = roles[rId];
                        role["name"] = roleCfg[rId]["Name"];
                        data.root.push(role);
                    }
                }
                data.totalProperty = data.root.length;
                me.getUserSoldierStore().proxy.data = data;
                me.getUserSoldierStore().load();

                /* 显示包裹数据 */
                data.root = [];
                var materialCfg = sgConfigs["material"];
                var materials = res["data"]["inventory"]["material"];
                for (var mId in materials) {
                    if (materials.hasOwnProperty(mId)) {
                        data.root.push({
                            name: materialCfg[mId]["Name"],
                            num: materials[mId]
                        });
                    }
                }
                var gemCfg = sgConfigs["gem"];
                var gems = res["data"]["inventory"]["gem"];
                for (var gId in gems) {
                    if (gems.hasOwnProperty(gId)) {
                        data.root.push({
                            name: gemCfg[gId]["Name"],
                            num: gems[gId]
                        });
                    }
                }
                data.root.sort(function(a, b){return b.num - a.num;});
                data.totalProperty = data.root.length;
                me.getUserInventoryStore().proxy.data = data;
                me.getUserInventoryStore().load();

            } else if (res["code"] == -1) { // 验证失败
                //me.getApplication().reLogin();
            }

        }, function(){
        });
    },

    /**
     * 编辑用户基本信息
     */
    onEditUserBasicData: function() {
        if (this.getApplication().checkAccess('status')) {
            var userStatusRecord = this.getUserStatusStore().getAt(0);
            if (userStatusRecord) {
                var editor = Ext.createWidget("userBasicDataEditor");
                editor.down('form').loadRecord(userStatusRecord);
                editor.show();
            }
        } else {
            Ext.MessageBox.alert("警告", "你没有该操作的访问权限！");
        }
    },

    onReloadUserStatus: function(sender, event) {
        var userStatusRecord = this.getUserStatusStore().getAt(0);
        if (!userStatusRecord) {
            return;
        }
        sender.up('form').loadRecord(userStatusRecord);
    },

    /**
     * 保存用户基本信息修改
     */
    onSaveUserStatus: function(sender, event) {
        var userStatusRecord = this.getUserStatusStore().getAt(0);
        if (!userStatusRecord) {
            return;
        }
        var form = sender.up('form');
        var values = form.getValues();
        for (var key in values) {
            if (values.hasOwnProperty(key)) {
                userStatusRecord.set(key, +values[key]);
            }
        }

        var changes = userStatusRecord.getChanges();
        var failureArr = [];
        async.eachSeries(Object.keys(changes), function(key, cb){
            var value = changes[key];
            sggm.utils.Http.request({
                act: 'gm',
                params: {
                    method: "status",
                    type: key,
                    value: value,
                    way: 'set'
                }
            }, function(res){
                if (res["code"] == 0) {
                    cb(null);
                } else {
                    failureArr.push(key);
                    cb(null);
                }
            }, function(){
                failureArr.push(key);
                cb(null);
            });
        }, function(err) {
            if (failureArr.length > 0) {
                // 这些数据保存失败，恢复
                var modified = userStatusRecord["modified"];
                for (var i= 0,j=failureArr.length; i<j;++i) {
                    var key = failureArr[i];
                    userStatusRecord.set(key, modified[key]);
                }
            }
            // 提交修改
            userStatusRecord.commit();
        });
    },

    onEditUserBasicDataSingle: function(sender, event) {
        if (this.checkReady('status')) {
            Ext.createWidget("userBasicDataSingleEditor").show();
        }
    },

    onUpdateUserDataSingle: function(sender, event) {
        var userStatusRecord = this.getUserStatusStore().getAt(0);
        var items = sender.up("window").items;
        var type = items.get(0).down("radio").getGroupValue();
        var way = items.get(1).down("radio").getGroupValue();
        var value = items.get(2).getValue();

        sggm.utils.Http.request({
            act: 'gm',
            params: {
                method: "status",
                type: type,
                way: way,
                value: value
            }
        }, function(res){
            if (res["code"] == 0) {
                if (type != "custom") {
                    if (way == "set") {
                        userStatusRecord.set(type, value);
                    } else {
                        var oldVal = +userStatusRecord.get(type);
                        userStatusRecord.set(type, +value + oldVal);
                    }
                    userStatusRecord.commit();
                }

                Ext.MessageBox.alert("提示", "操作成功");
            } else {
                Ext.MessageBox.alert("提示", res["desc"]);
            }
        }, function(){
        });
    },

    onAddCash: function(sender, event) {
        if (this.checkReady('add_cash')) {
            Ext.createWidget("addCashWindow").show();
        }
    },

    onEditCustom: function(sender, event) {
        if (this.checkReady('custom_status')) {
            Ext.createWidget("editCustomWindow").show();
        }
    },

    ///////////////////////////////////////////////////////////////////////////

    onBlockUser: function(sender, event) {
        console.log("onBlockUser");
        if (this.getApplication().checkAccess('block')
            && this.getUserStatusStore().getAt(0)) {
            sggm.utils.Http.request({
                act: 'gm',
                params: {
                    method: "block"
                }
            }, function(res){
                Ext.MessageBox.alert("提示", res["desc"]);
            }, function(){

            });
        }
    },

    onUnblockUser: function(sender, event) {
        console.log("onUnblockUser");
        if (this.getApplication().checkAccess('unblock')
            && this.getUserStatusStore().getAt(0)) {
            sggm.utils.Http.request({
                act: 'gm',
                params: {
                    method: "unblock"
                }
            }, function(res){
                Ext.MessageBox.alert("提示", res["desc"]);
            }, function(){

            });
        }
    },

    onUnblockLimitUser: function(sender, event) {
        console.log("onUnblockLimitUser");
        if (this.getApplication().checkAccess('unblock_limit')
            && this.getUserStatusStore().getAt(0)) {
            sggm.utils.Http.request({
                act: 'gm',
                params: {
                    method: "unblock_limit"
                }
            }, function(res){
                if (res["code"] == 0) {
                    Ext.MessageBox.alert("提示", "操作成功");
                } else {
                    Ext.MessageBox.alert("提示", res["desc"]);
                }
            }, function(){

            });
        }
    },

    onBlockUserTalk: function(sender, event) {
        console.log("onBlockUserTalk");
        if (this.getApplication().checkAccess('block_talk')
            && this.getUserStatusStore().getAt(0)) {
            sggm.utils.Http.request({
                act: 'gm',
                params: {
                    method: "block_talk"
                }
            }, function(res){
                if (res["code"] == 0) {
                    Ext.MessageBox.alert("提示", "操作成功");
                } else {
                    Ext.MessageBox.alert("提示", res["desc"]);
                }
            }, function(){

            });
        }
    },

    onUnblockUserTalk: function(sender, event) {
        console.log("onUnblockUserTalk");
        if (this.getApplication().checkAccess('block_talk')
            && this.getUserStatusStore().getAt(0)) {
            sggm.utils.Http.request({
                act: 'gm',
                params: {
                    method: "unblock_talk"
                }
            }, function(res){
                if (res["code"] == 0) {
                    Ext.MessageBox.alert("提示", "操作成功");
                } else {
                    Ext.MessageBox.alert("提示", res["desc"]);
                }
            }, function(){

            });
        }
    },

    onClearUserInventory: function(sender, event) {
        console.log("onClearUserInventory");
        if (this.getApplication().checkAccess('clear_inventory')
            && this.getUserStatusStore().getAt(0)) {
            Ext.MessageBox.confirm('提示', '你确认要清空包裹，这是个坑！', function(btn){
                if (btn == 'yes') {
                    sggm.utils.Http.request({
                        act: 'gm',
                        params: {
                            method: "clear_inventory"
                        }
                    }, function(res){
                        if (res["code"] == 0) {
                            Ext.MessageBox.alert("提示", "操作成功");
                        } else {
                            Ext.MessageBox.alert("提示", res["desc"]);
                        }
                    }, function(){

                    });
                }
            });
        }
    },

    onDiamond: function(sender, event) {
        console.log("onDiamond");
        if (this.getApplication().checkAccess('diamond')
            && this.getUserStatusStore().getAt(0)) {
            sggm.utils.Http.request({
                act: 'gm',
                params: {
                    method: "diamond"
                }
            }, function(res){
                if (res["code"] == 0) {
                    Ext.MessageBox.alert("提示", "操作成功");
                } else {
                    Ext.MessageBox.alert("提示", res["desc"]);
                }
            }, function(){

            });
        }
    },

    onQueryUserMessage: function(sender, event) {
        console.log("onQueryUserMessage");
        var me = this;
        if (this.getApplication().checkAccess('message')
            && this.getUserStatusStore().getAt(0)) {
            sggm.utils.Http.request({
                act: 'gm',
                params: {
                    method: "message"
                }
            }, function(res){
                if (res["code"] == 0) {
                    var ims = res["data"]["ims"];
                    var gmPanel = Ext.getCmp("gmPanel");
                    var messageList = gmPanel.down("messageList");
                    if (!messageList)  {
                        gmPanel.add({xtype:"messageList"});
                        messageList = gmPanel.down("messageList");
                    }
                    me.getMessageStore().proxy.data = {
                        totalProperty: ims.length,
                        root: ims
                    };
                    me.getMessageStore().load();
                }
            }, function(){

            });
        }
    },

    onAutoStrong: function(sender, event) {
        console.log("onAutoStrong");
        if (this.getApplication().checkAccess('autoStrong')
            && this.getUserStatusStore().getAt(0)) {
            sggm.utils.Http.request({
                act: 'gm',
                params: {
                    method: "autoStrong"
                }
            }, function(res){
                if (res["code"] == 0) {
                    Ext.MessageBox.alert("提示", "操作成功");
                } else {
                    Ext.MessageBox.alert("提示", res["desc"]);
                }
            }, function(){

            });
        }
    },

    onSetUser: function(sender, event) {
        console.log("onSetUser");
        var jsonWin = Ext.createWidget("setUserWindow").show();
    },

    onSaveSetUser: function(sender, event) {
        console.log("onSaveSetUser");
        var userData = sender.up("window").down("textarea").getValue();
        sggm.utils.Http.request({
            act: 'gm',
            //method: 'POST',
            params: {
                method: "set_user",
                value: userData
            }
        }, function(res){
            if (res["code"] == 0) {
                Ext.MessageBox.alert("提示", "操作成功");
            } else {
                Ext.MessageBox.alert("提示", res["desc"]);
            }
        }, function(){

        });
    },

    ///////////////////////////////////////////////////////////////////////////

    onUpdateUserMark: function(sender, event) {
        if (this.checkReady('mark')) {
            Ext.createWidget("updateMarkWindow").show();
        }
    },

    onSaveUserMark: function(sender, event) {
        var items = sender.up("window").items;
        sggm.utils.Http.request({
            act: 'gm',
            params: {
                method: "mark",
                "type": items.get(0).getValue(),
                "value": items.get(1).getValue()
            }
        }, function(res){
            if (res["code"] == 0) {
                Ext.MessageBox.alert("提示", "操作成功");
            } else {
                Ext.MessageBox.alert("提示", res["desc"]);
            }
        }, function(){
        });
    },

    onUpdateUserTask: function(sender, event) {
        if (this.checkReady('mark')) {
            Ext.createWidget("updateTaskWindow").show();
        }
    },

    onSaveUserTask: function(sender, event) {
        var items = sender.up("window").items;
        sggm.utils.Http.request({
            act: 'gm',
            params: {
                method: "task",
                "type": items.get(0).getValue(),
                "value": items.get(1).getValue()
            }
        }, function(res){
            if (res["code"] == 0) {
                Ext.MessageBox.alert("提示", "操作成功");
            } else {
                Ext.MessageBox.alert("提示", res["desc"]);
            }
        }, function(){
        });
    },

    onShowSendInventory: function(sender, event) {
        if (this.checkReady('inventory')) {
            Ext.createWidget("sendInventoryWindow").show();
        }
    },

    onSendInventoryWindowCombosRender: function(combo) {
        var me = this;
        if (combo.name == "type") {
            combo.on("select", function(comboBox){
                var type = comboBox.getValue();
                var firstId = null;
                var data = me.getInventoryComboData(type);
                if (data.length > 0) {
                    firstId = data[0][0];
                }
                var items = comboBox.up("window").items;
                var gemMaterialCombo = items.get(1);
                gemMaterialCombo.store.proxy.data = data;
                gemMaterialCombo.store.load();
                gemMaterialCombo.setValue(firstId);
            });
        } else {
            var firstId = null;
            var data = this.getInventoryComboData("material");
            if (data.length > 0) {
                firstId = data[0][0];
            }
            combo.store.proxy.data = data;
            combo.store.load();
            combo.setValue(firstId);
        }
    },

    getInventoryComboData: function(type) {
        var inventorys = this.getApplication().sgConfigs[type];
        var data = [];
        for (var mId in inventorys) {
            if (inventorys.hasOwnProperty(mId)) {
                data.push([
                    mId, inventorys[mId]["Name"]
                ]);
            }
        }
        return data;
    },

    onSendInventory: function(sender, event) {
        var items = sender.up("window").items;
        sggm.utils.Http.request({
            act: 'gm',
            params: {
                method: "inventory",
                "type": items.get(0).getValue(),
                "id": items.get(1).getValue(),
                "num": items.get(2).getValue()
            }
        }, function(res){
            if (res["code"] == 0) {
                Ext.MessageBox.alert("提示", "操作成功");
            } else {
                Ext.MessageBox.alert("提示", res["desc"]);
            }
        }, function(){
        });
    },

    onShowSendEquipWindow: function(sender, event) {
        if (this.checkReady('equip')) {
            Ext.createWidget("sendEquipWindow").show();
        }
    },

    onSendEquip: function(sender, event) {
        var win = sender.up("window");
        sggm.utils.Http.request({
            act: 'gm',
            params: {
                method: "equip",
                "type": win.down('radio').getGroupValue(),
                "level": win.down('numberfield').getValue()
            }
        }, function(res){
            if (res["code"] == 0) {
                Ext.MessageBox.alert("提示", "操作成功");
            } else {
                Ext.MessageBox.alert("提示", res["desc"]);
            }
        }, function(){
        });
    },

    onShowUpdateFighter: function(sender, event){
        if (this.checkReady('userrole')) {
            Ext.createWidget("updateFighterWindow").show();
        }
    },

    onUpdateFighterWindowComboRender: function(combo) {
        var roleCfg = this.getApplication().sgConfigs["role"];
        var roles = this.getApplication().playerData["role"];

        var firstId = null;
        var data = [];
        for (var rId in roles) {
            if (roles.hasOwnProperty(rId)) {
                data.push([
                    rId, roleCfg[rId]["Name"]
                ]);
            }
        }
        if (data.length > 0) firstId = data[0][0];
        combo.store.proxy.data = data;
        combo.store.load();
        combo.setValue(firstId);
    },

    onUpdateFighter: function(sender, event) {
        var win = sender.up("window");
        sggm.utils.Http.request({
            act: 'gm',
            params: {
                method: "userrole",
                "type": win.down('radio').getGroupValue(),
                "id": win.down('combo').getValue(),
                value: win.down('numberfield').getValue()
            }
        }, function(res){
            if (res["code"] == 0) {
                Ext.MessageBox.alert("提示", "操作成功");
            } else {
                Ext.MessageBox.alert("提示", res["desc"]);
            }
        }, function(){
        });
    },

    onShowSendFighter: function(sender, event){
        if (this.checkReady('role')) {
            Ext.createWidget("sendFighterWindow").show();
        }
    },

    onSendFighterWindowComboRender: function(combo) {
        var firstId = null;
        var data = this.getFighterComboData();
        if (data.length > 0) {
            firstId = data[0][0];
        }
        combo.store.proxy.data = data;
        combo.store.load();
        combo.setValue(firstId);
    },

    getFighterComboData: function() {
        var roles = this.getApplication().sgConfigs["role"];
        var data = [];
        for (var rId in roles) {
            if (roles.hasOwnProperty(rId)) {
                data.push([
                    rId, roles[rId]["Name"]
                ]);
            }
        }
        return data;
    },

    onSendFighter: function(sender, event) {
        var win = sender.up("window");
        sggm.utils.Http.request({
            act: 'gm',
            params: {
                method: "role",
                id: win.down('combo').getValue(),
                num: win.down('numberfield').getValue()
            }
        }, function(res){
            if (res["code"] == 0) {
                Ext.MessageBox.alert("提示", "操作成功");
            } else {
                Ext.MessageBox.alert("提示", res["desc"]);
            }
        }, function(){
        });
    },

    onShowUpdateVIP: function(sender, event) {
        if (this.checkReady('vip')) {
            Ext.createWidget("updateVipWindow").show();
        }
    },

    onSaveVIP: function(sender, event) {
        var userStatusRecord = this.getUserStatusStore().getAt(0);
        var win = sender.up("window");
        var vipVal = win.down('numberfield').getValue();
        sggm.utils.Http.request({
            act: 'gm',
            params: {
                method: "vip",
                value: vipVal
            }
        }, function(res){
            if (res["code"] == 0) {
                userStatusRecord.set("vip", +vipVal);
                userStatusRecord.commit();
                Ext.MessageBox.alert("提示", "操作成功");
            } else {
                Ext.MessageBox.alert("提示", res["desc"]);
            }
        }, function(){
        });
    },

    onShowCharge: function(sender, event) {
        if (this.checkReady('pay')) {
            Ext.createWidget("chargeWindow").show();
        }
    },

    onCharge: function(sender, event) {
        var userStatusRecord = this.getUserStatusStore().getAt(0);
        var win = sender.up("window");
        var cashVal = win.down('numberfield').getValue();
        sggm.utils.Http.request({
            act: 'gm',
            params: {
                method: "pay",
                cash: cashVal
            }
        }, function(res){
            if (res["code"] == 0) {
                var oldVal = +userStatusRecord.get("cash");
                userStatusRecord.set("cash", +cashVal + oldVal);
                userStatusRecord.commit();
                Ext.MessageBox.alert("提示", "操作成功");
            } else {
                Ext.MessageBox.alert("提示", res["desc"]);
            }
        }, function(){
        });
    },

    onShowUpdateLeague: function(sender, event) {
        if (this.checkReady('league')) {
            Ext.createWidget("updateLeague").show();
        }
    },

    onSaveLeague: function(sender, event) {
        var win = sender.up("window");
        sggm.utils.Http.request({
            act: 'gm',
            params: {
                method: "league",
                name: win.down('textfield').getValue(),
                type: win.down('radio').getGroupValue(),
                value: win.down('numberfield').getValue()
            }
        }, function(res){
            if (res["code"] == 0) {
                Ext.MessageBox.alert("提示", "操作成功");
            } else {
                Ext.MessageBox.alert("提示", res["desc"]);
            }
        }, function(){
        });
    },

    onShowOtherCmd: function(sender, event) {
        if (this.checkReady('other')) {
            Ext.createWidget("otherWindow").show();
        }
    },

    onSaveOther: function(sender, event) {
        var win = sender.up("window");
        sggm.utils.Http.request({
            act: 'gm',
            params: {
                method: "other",
                command: win.down('radio').getGroupValue(),
                value: win.down('textfield').getValue()
            }
        }, function(res){
            if (res["code"] == 0) {
                Ext.MessageBox.alert("提示", "操作成功");
            } else {
                Ext.MessageBox.alert("提示", res["desc"]);
            }
        }, function(){
        });
    },

    onShowChangeRole: function(sender, event) {
        sggm.utils.Http.request({
            act: 'gm',
            params: {
                method: "get_uid"
            }
        }, function(res){
            if (res["code"] == 0) {
                if (res["data"].length == 0) {
                    Ext.MessageBox.alert("提示", "没有可以更新的UID");
                } else {
                    var data = [];
                    res["data"].forEach(function(uid){
                        var sid = 0;
                        if( uid > 100000000 ) {
                            if( uid < 101000000 ) {
                                sid = 46
                            }else{
                                sid = Math.floor((uid-100000000)/100000);
                            }
                        }else{
                            sid = Math.floor(uid/1000000);
                        }

                        sid = sid - 1;
                        data.push([
                            uid, sid + "服-" + uid
                        ]);
                    });

                    if (data.length > 0) {
                        var changeRoleWin = Ext.createWidget("changeRoleWindow");
                        var combo = changeRoleWin.down("combo");
                        combo.store.proxy.data = data;
                        combo.store.load();
                        changeRoleWin.show();
                    }
                }
            } else {
                Ext.MessageBox.alert("提示", res["desc"]);
            }
        }, function(){
        });
    },

    onChangeRole: function(sender, event) {
        var win = sender.up("window");
        sggm.utils.Http.request({
            act: 'gm',
            params: {
                method: "change_uid",
                new_uid: win.down('combo').getValue()
            }
        }, function(res){
            if (res["code"] == 0) {
                Ext.MessageBox.alert("提示", "操作成功");
            } else {
                Ext.MessageBox.alert("提示", res["desc"]);
            }
        }, function(){
        });
    },

    onShowJSON: function(sender, event) {
        if (this.checkReady('get')) {
            var jsonWin = Ext.createWidget("userJSONWindow");
            jsonWin.down("textarea").setValue(JSON.stringify(this.getApplication().playerData));
            jsonWin.show();
        }
    },

    onSaveEditCustom: function(sender, event) {
        var win = sender.up("window");
        sggm.utils.Http.request({
            act: 'gm',
            params: {
                method: "custom_status",
                value: win.down('textfield').getValue()
            }
        }, function(res){
            if (res["code"] == 0) {
                Ext.MessageBox.alert("提示", "操作成功");
            } else {
                Ext.MessageBox.alert("提示", res["desc"]);
            }
        }, function(){
        });
    },

    onAddCashWindowAddCash: function(sender, event) {
        var win = sender.up("window");
        var textField = win.down("textfield");
        var numToAdd = +(textField.getValue());
        if (isNaN(numToAdd)) {
            Ext.MessageBox.alert("提示","请输入数字");
            return;
        }
        sggm.utils.Http.request({
            act: 'gm',
            params: {
                method: "add_cash",
                value: numToAdd
            }
        }, function(res){
            if (res["code"] == 0) {
                Ext.MessageBox.alert("提示", "操作成功");
            } else {
                Ext.MessageBox.alert("提示", res["desc"]);
            }
        }, function(){
        });
    },
    ///////////////////////////////////////////////////////////////////////////

    checkReady: function(act) {
        if (!this.getUserStatusStore().getAt(0)) {
            return false;
        }
        if (this.getApplication().checkAccess(act)) {
            return true;
        }
        Ext.MessageBox.alert("警告", "你没有该操作的访问权限！");
        return false;
    }
});
